package fr.umlv.android.caveAvin.sqlite;

import java.util.ArrayList;

import fr.umlv.android.caveAVin.beans.WineCellar;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class WineCellarBDD extends MyProjectBDD {


	private static final long serialVersionUID = 1L;
	
	private static final String TABLE_WINE_CELAR ="table_wine_celar";
	
	private static final String COL_ID_WINE_CELAR = "Id_wine_celar";
	
	private static final int NUM_COL_ID = 0;
	
	private static final String COL_NAME  ="Name";
	
	private static final int NUM_COL_NAME  =1;
	
	private static final String COL_ADDRESS  ="Address";
	
	private static final int NUM_COL_ADDRESS  =2;
	
	private static final String COL_POSTALCODE  ="Postal_Code";
	
	private static final int NUM_COL_POSTALCODE  =3;
	
	private static final String COL_CITY  ="City";
	
	private static final int NUM_COL_CITY  =4;
	
	private static final String COL_COUNTRY  ="Country";
	
	private static final int NUM_COL_COUNTRY  =5;
	
	private static final String COL_DATECREATION = "Date_Creation";
	
	private static final int NUM_COL_DATECREATION = 6;
	
	private static final String COL_CAPACITY  ="Capacity";
	
	private static final int NUM_COL_CAPACITY  =7;
	
	private static final String COL_NBBOTTLE  ="NB_Bottle";
	
	private static final int NUM_COL_NBBOTTLE  =8;
	
	//Foreign keys
	private static final String COL_ID_USER_FK = "Id_user";

	public WineCellarBDD(Context context) {
		super(context);
	}
	
	public static long insertWineCellar(WineCellar wineCellar, int idUser){
		//Création d'un ContentValues (fonctionne comme une HashMap)
		ContentValues values = new ContentValues();
		//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
		
		values.put(COL_NAME, wineCellar.getName());
		values.put(COL_ADDRESS, wineCellar.getAddress());
		values.put(COL_POSTALCODE, wineCellar.getPostalCode());
		values.put(COL_CITY, wineCellar.getCity());
		values.put(COL_COUNTRY, wineCellar.getCountry());
		values.put(COL_DATECREATION, wineCellar.getDateCreation());
		values.put(COL_CAPACITY, wineCellar.getCapacity());
		values.put(COL_NBBOTTLE, wineCellar.getNbBottlePresent());
		values.put(COL_ID_USER_FK, idUser);
		
		//on insère l'objet dans la BDD via le ContentValues
		return bdd.insert(TABLE_WINE_CELAR, null, values);
	}
	
	
	public static ArrayList<WineCellar> getAllWineCellar () {
		Cursor c = bdd.query(TABLE_WINE_CELAR ,new String[] {COL_ID_WINE_CELAR, COL_NAME, COL_ADDRESS, COL_POSTALCODE, COL_CITY,COL_COUNTRY, COL_DATECREATION, COL_CAPACITY, COL_NBBOTTLE, COL_ID_USER_FK }
					,"", null, null, null, null);
		if (c == null || c.getCount() == 0 ) {
			return null;
		}
		return cursorToWineCellar(c);
	}
	
	public static ArrayList<WineCellar> getAllWineCellarForCurrentUser (int idUser) {
		Cursor c = bdd.query(TABLE_WINE_CELAR ,new String[] {COL_ID_WINE_CELAR, COL_NAME, COL_ADDRESS, COL_POSTALCODE, COL_CITY,COL_COUNTRY, COL_DATECREATION, COL_CAPACITY, COL_NBBOTTLE, COL_ID_USER_FK }
					,COL_ID_USER_FK + " = " + idUser, null, null, null, null);
		
		return cursorToWineCellar(c);
	}


	private static ArrayList<WineCellar> cursorToWineCellar(Cursor c) {
		ArrayList<WineCellar> list = new ArrayList<WineCellar>();
		WineCellar wineCellar = null;
		if ( c == null || c.getCount() == 0)
			return list;
		for (int i=0;i< c.getCount(); i++) {
			c.moveToPosition(i);
			wineCellar = new WineCellar();
			wineCellar.setId(c.getInt(NUM_COL_ID));
			wineCellar.setName(c.getString(NUM_COL_NAME));
			wineCellar.setAddress(c.getString(NUM_COL_ADDRESS));
			wineCellar.setPostalCode(c.getString(NUM_COL_POSTALCODE));
			wineCellar.setCity(c.getString(NUM_COL_CITY));
			wineCellar.setCountry(c.getString(NUM_COL_COUNTRY));
			wineCellar.setDateCreation(c.getString(NUM_COL_DATECREATION));
			wineCellar.setCapacity(c.getInt(NUM_COL_CAPACITY));
			wineCellar.setNbBottlePresent(c.getInt(NUM_COL_NBBOTTLE));
			list.add(wineCellar);
		}
		c.close();
		return list;
	}
}
